package com.example.j202411.views.Fragment;


import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;


import com.example.j202411.R;
import com.example.j202411.adapter.PetAdapter;
import com.example.j202411.contract.PetsContact;
import com.example.j202411.databinding.FragmentPetsBinding;
import com.example.j202411.Model.PetEntity;
import com.example.j202411.viewmodel.PetsModel;

import java.util.ArrayList;
import java.util.List;

public class FragmentHome extends Fragment implements PetsContact.PetsView, View.OnClickListener {
    private PetsContact.PetsModel petsModel;
    private PetAdapter adapter;
    private List<PetEntity.Result.ListDate> mlist;
    private FragmentPetsBinding binding;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = FragmentPetsBinding.inflate(getLayoutInflater());
        return binding.getRoot();

    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
    }

    private void initView() {
        petsModel = new PetsModel(this, requireContext());
        binding.pe1.setSelected(true);
        petsModel.getData(0, null);
        mlist = new ArrayList<>();
        binding.recyclepets.setLayoutManager(new GridLayoutManager(getContext(), 2));
        adapter = new PetAdapter(mlist, getContext());
        binding.recyclepets.setAdapter(adapter);
        binding.pe1.setOnClickListener(this);
        binding.pe2.setOnClickListener(this);
        binding.pe3.setOnClickListener(this);
        binding.pe4.setOnClickListener(this);
        binding.pe5.setOnClickListener(this);
        binding.pSearchI.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.pe1) {
            petsModel.getData(0, null);
            setButtonSelected(R.id.pe1);
        } else if (id == R.id.pe2) {
            petsModel.getData(1, null);
            setButtonSelected(R.id.pe2);
        } else if (id == R.id.pe3) {
            petsModel.getData(2, null);
            setButtonSelected(R.id.pe3);
        } else if (id == R.id.pe4) {
            petsModel.getData(3, null);
            setButtonSelected(R.id.pe4);
        } else if (id == R.id.pe5) {
            petsModel.getData(4, null);
            setButtonSelected(R.id.pe5);
        } else if (id == R.id.p_search_i) {
            String name = binding.pSearchE.getText().toString().trim();
            Log.d("185184", name);
            petsModel.getData(0, name);
        }
    }

    private void setButtonSelected(int selectedId) {
        binding.pe1.setSelected(selectedId == R.id.pe1);
        binding.pe2.setSelected(selectedId == R.id.pe2);
        binding.pe3.setSelected(selectedId == R.id.pe3);
        binding.pe4.setSelected(selectedId == R.id.pe4);
        binding.pe5.setSelected(selectedId == R.id.pe5);
    }

    @Override
    public void showData(PetEntity petEntity) {
        if (petEntity.getError_code() == 0) {
            Log.d("1231", "showData: ");
            mlist = petEntity.getResult().getList();
            Log.d("mlist000000000000000", String.valueOf(mlist.size()));
            adapter.updateData(mlist);
        }
    }
}